﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Globalization;
using Libs.HR;

public partial class Pages_Hr_Employees_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AppUtils.CheckLogin();
        Title = Title + " - Cập nhật thông tin nhân sự";
        if (!IsPostBack)
        {
            btDelete.Attributes.Add("onclick", "return confirm('Bạn có đồng ý xóa?');");
            GetDropDownList();
            init();
        }
    }

    private void GetDropDownList()
    {
        // Department
        var _Department = new Departments();
        drpDepartment.DataSource = _Department.GetTList();
        drpDepartment.DataBind();
        drpDepartment.Items.Insert(0, new ListItem("Chọn phòng ban:", "0"));

        // Office
        var _Office = new Office();
        drpOffice.DataSource = _Office.GetTList();
        drpOffice.DataBind();
        drpOffice.Items.Insert(0, new ListItem("Chọn văn phòng:", "0"));

        // Loại hợp đồng
        var _ContractType = new ContractType();
        drpContractType.DataSource = _ContractType.GetTList();
        drpContractType.DataBind();
        drpContractType.Items.Insert(0, new ListItem("Chọn loại hợp đồng:", "0"));

        // Education
        var _Education = new Education();
        drpEducation.DataSource = _Education.GetTList();
        drpEducation.DataBind();
        drpEducation.Items.Insert(0, new ListItem("Chọn trình độ học vấn:", "0"));
    }

    private void init()
    {
        // Employee
        var _Employee = new Employees() { EmployeeID = AppUtils.Request("id") };
        _Employee = _Employee.Get();
        if (_Employee == null) Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesList);

        txtFirstName.Text = _Employee.FirstName;
        txtLastName.Text = _Employee.LastName;
        txtManualID.Text = _Employee.ManualID;
        if (_Employee.Avatar > 0)
        {
            imgAvatar.ImageUrl = Constant.ADMIN_PATH + Resources.Url.FileDataView + "?id=" + _Employee.Avatar.ToString();
            imgAvatar.Height = 80;
        }
        lblFileID.Text = _Employee.Avatar.ToString();
        txtBirthday.Text = _Employee.Birthday.ToString("dd/MM/yyyy");
        txtEmail.Text = _Employee.Email;
        txtJoinedTime.Text = _Employee.JoinedTime.ToString("dd/MM/yyyy");
        txtWorkPhone.Text = _Employee.WorkPhone;
        txtMobile.Text = _Employee.Mobile;

        try
        {
            drpGender.SelectedValue = _Employee.Gender.ToString();
        }
        catch 
        { 
        }
        try
        {
            drpDepartment.SelectedValue = _Employee.DepartmentID.ToString();
        }
        catch
        {
        }
        try
        {
            drpContractType.SelectedValue = _Employee.ContractTypeID.ToString();
        }
        catch
        {
        }
        try
        {
            drpOffice.SelectedValue = _Employee.OfficeID.ToString();
        }
        catch
        {
        }

        // Infor
        var _EmployeeInfo = new EmployeeInfo() { EmployeeID = AppUtils.Request("id") };
        _EmployeeInfo = _EmployeeInfo.Get();
        if (_EmployeeInfo != null)
        {
            txtPrivateEmail.Text = _EmployeeInfo.PrivateEmail;
            cbxMarried.Checked = _EmployeeInfo.Married;
            txtHomePhone.Text = _EmployeeInfo.HomePhone;
            txtCurrentAddress.Text = _EmployeeInfo.CurrentAddress;
            txtPermanentAddress.Text = _EmployeeInfo.PermanentAddress;
            txtOriginalAddress.Text = _EmployeeInfo.OriginalAddress;
            txtIDNumber.Text = _EmployeeInfo.IDNumber;
            txtIDNumberTime.Text = _EmployeeInfo.IDNumberTime.ToString("dd/MM/yyyy");
            txtIDNumberCity.Text = _EmployeeInfo.IDNumberCity;
            drpEducation.SelectedValue = _EmployeeInfo.EducationID.ToString();
            txtEthnic.Text = _EmployeeInfo.Ethnic;
            txtReligion.Text = _EmployeeInfo.Religion;
            txtDescription.Text = _EmployeeInfo.Description;
        }

        // Contract
        var _Contract = new Contracts();
        rptContractList.DataSource = _Contract.GetTList(AppUtils.Request("id"));
        rptContractList.DataBind();

        // Family
        var _Family = new Family();
        rptFamilyList.DataSource = _Family.GetTList(AppUtils.Request("id"));
        rptFamilyList.DataBind();


        // Profile
        var _ProfileLog = new ProfileLog() { EmployeeID = AppUtils.Request("id") };
        rptList.DataSource = _ProfileLog.GetTList(_ProfileLog.EmployeeID);
        rptList.DataBind();

        rptProfileList.DataSource = _ProfileLog.GetTListRemaining(_ProfileLog.EmployeeID);
        rptProfileList.DataBind();
    }

    protected void btUpdate_Click(object sender, EventArgs e)
    {
        CultureInfo cul = new CultureInfo("vi-VN");

        var _Employee = new Employees() { EmployeeID = AppUtils.Request("id") };
        _Employee.FirstName = txtFirstName.Text.Trim();
        _Employee.LastName = txtLastName.Text.Trim();
        _Employee.ManualID = txtManualID.Text.Trim();
        _Employee.Avatar = Convert.ToInt32(lblFileID.Text);
        _Employee.Birthday = Convert.ToDateTime(txtBirthday.Text, cul);
        _Employee.Gender = Convert.ToInt32(drpGender.SelectedValue);
        _Employee.Email = txtEmail.Text.Trim();
        _Employee.DepartmentID = Convert.ToInt32(drpDepartment.SelectedValue);
        _Employee.OfficeID = Convert.ToInt32(drpOffice.SelectedValue);
        _Employee.JoinedTime = Convert.ToDateTime(txtJoinedTime.Text, cul);
        _Employee.ContractTypeID = Convert.ToInt32(drpContractType.SelectedValue);
        _Employee.WorkPhone = txtWorkPhone.Text.Trim();
        _Employee.Mobile = txtMobile.Text.Trim();
        _Employee.Update();

        Redirect();
    }

    protected void btDelete_Click(object sender, EventArgs e)
    {
        Redirect();
    }

    protected void btCancel_Click(object sender, EventArgs e)
    {
        Redirect();
    }

    private void Redirect()
    {
        if (string.IsNullOrEmpty(Request["u"]))
        {
            Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesList);
        }
        else
        {
            Response.Redirect(Request["u"]);
        }
    }

    protected void btImgUpload_Click(object sender, EventArgs e)
    {
        if (fuFileUpload.PostedFile != null && fuFileUpload.PostedFile.ContentLength != 0)
        {
            var _FileData = new FileData();
            _FileData.FileType = fuFileUpload.PostedFile.ContentType;
            _FileData.Size = fuFileUpload.PostedFile.ContentLength;
            _FileData.FileName = Path.GetFileName(fuFileUpload.PostedFile.FileName);

            _FileData.Data = new byte[_FileData.Size];
            fuFileUpload.PostedFile.InputStream.Read(_FileData.Data, 0, _FileData.Size);
            _FileData.Add();
            lblFileID.Text = _FileData.FileID.ToString();
            imgAvatar.ImageUrl = Constant.ADMIN_PATH + Resources.Url.FileDataView + "?id=" + lblFileID.Text;
            imgAvatar.Height = 80;
        }
    }

    protected void btInforUpdate_Click(object sender, EventArgs e)
    {
        CultureInfo cul = new CultureInfo("vi-VN");

        var _EmployeeInfo = new EmployeeInfo() { EmployeeID = AppUtils.Request("id") };
        _EmployeeInfo = _EmployeeInfo.Get();
        if (_EmployeeInfo == null) _EmployeeInfo = new EmployeeInfo();

        _EmployeeInfo.PrivateEmail = txtPrivateEmail.Text.Trim().ToLower();
        _EmployeeInfo.Married = cbxMarried.Checked;
        _EmployeeInfo.HomePhone = txtHomePhone.Text.Trim();
        _EmployeeInfo.CurrentAddress = txtCurrentAddress.Text.Trim();
        _EmployeeInfo.PermanentAddress = txtPermanentAddress.Text.Trim();
        _EmployeeInfo.OriginalAddress = txtOriginalAddress.Text.Trim();
        _EmployeeInfo.IDNumber = txtIDNumber.Text.Trim();
        _EmployeeInfo.IDNumberTime = Convert.ToDateTime(txtIDNumberTime.Text, cul);
        _EmployeeInfo.IDNumberCity = txtIDNumberCity.Text.Trim();
        _EmployeeInfo.EducationID = Convert.ToInt32(drpEducation.SelectedValue);
        _EmployeeInfo.Ethnic = txtEthnic.Text.Trim();
        _EmployeeInfo.Religion = txtReligion.Text.Trim();
        _EmployeeInfo.Description = txtDescription.Text.Trim();

        if (_EmployeeInfo.EmployeeID > 0)
        {
            _EmployeeInfo.Update();
        }
        else
        {
            _EmployeeInfo.EmployeeID = AppUtils.Request("id");
            _EmployeeInfo.Add();
        }
    }

    protected void btInforCancel_Click(object sender, EventArgs e)
    {

    }

    protected void btContractAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.ContractsAdd + "?id=" + Request["id"]);
    }

    protected void btFamilyAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.FamilyAdd + "?id=" + Request["id"]);
    }

    private void BindDataProfile()
    {
        var _ProfileLog = new ProfileLog() { EmployeeID = AppUtils.Request("id") };
        rptList.DataSource = _ProfileLog.GetTList(_ProfileLog.EmployeeID);
        rptList.DataBind();

        rptProfileList.DataSource = _ProfileLog.GetTListRemaining(_ProfileLog.EmployeeID);
        rptProfileList.DataBind();
    }

    protected void btProfileUpdate_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rptList.Items.Count; i++)
        {
            var _ProfileLog = new ProfileLog() { EmployeeID = AppUtils.Request("id") };
            CheckBox cbx = (CheckBox)rptList.Items[i].FindControl("cbxSelect");
            TextBox txt = (TextBox)rptList.Items[i].FindControl("txtDeliveryTime");
            Label lbl = (Label)rptList.Items[i].FindControl("lblLogID");
            
            _ProfileLog.LogID = Convert.ToInt32(lbl.Text);
            _ProfileLog = _ProfileLog.Get();

            if (txt.Text.Trim().Length > 0)
            {
                _ProfileLog.DeliveryTime = Convert.ToDateTime(txt.Text, new CultureInfo("vi-VN"));
                _ProfileLog.Status = true;
            }
            else
            {
                _ProfileLog.DeliveryTime = DateTime.MinValue;
                _ProfileLog.Status = false;
            }
            _ProfileLog.Update();
        }

        BindDataProfile();
    }

    protected void btProfileDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rptList.Items.Count; i++)
        {
            CheckBox cbx = (CheckBox)rptList.Items[i].FindControl("cbxSelect");
            if (cbx.Checked)
            {
                var _ProfileLog = new ProfileLog() { EmployeeID = AppUtils.Request("id") };
                Label lbl = (Label)rptList.Items[i].FindControl("lblLogID");
                _ProfileLog.LogID = Convert.ToInt32(lbl.Text);
                _ProfileLog.Delete();
            }
        }

        BindDataProfile();
    }

    protected void btProfileAdd_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rptProfileList.Items.Count; i++)
        {
            var _ProfileLog = new ProfileLog() { EmployeeID = AppUtils.Request("id") };
            CheckBox cbx = (CheckBox)rptProfileList.Items[i].FindControl("cbxSelect");
            TextBox txt = (TextBox)rptProfileList.Items[i].FindControl("txtDeliveryTime");
            if (cbx.Checked)
            {
                Label lbl = (Label)rptProfileList.Items[i].FindControl("lblProfileID");
                _ProfileLog.ProfileID = Convert.ToInt32(lbl.Text);
                if (txt.Text.Trim().Length > 0)
                {
                    _ProfileLog.DeliveryTime = Convert.ToDateTime(txt.Text, new CultureInfo("vi-VN"));
                    _ProfileLog.Status = true;
                }
                else
                {
                    _ProfileLog.Status = false;
                }

                _ProfileLog.Add();
            }
        }

        BindDataProfile();
    }
}
